package com.wu.creditmanager.dialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import com.wu.creditmanager.R;
import com.wu.creditmanager.db.CreditsDbAdapter;
import com.wu.creditmanager.entity.Credit;
import com.wu.creditmanager.util.LayoutUtil;

public class BillFilterDialog extends Dialog implements OnClickListener,
		OnItemSelectedListener {

	private Spinner creditSp;
	private Spinner monthSp;
	private CheckBox paidCheck;
	private CheckBox notPaidCheck;
	private CreditsDbAdapter dbAdapter;
	private CallBack callBack;

	public BillFilterDialog(Context context, CallBack callBack) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.bill_filter_dialog);
		setCanceledOnTouchOutside(true);
		this.callBack = callBack;
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		paidCheck = (CheckBox) findViewById(R.id.check_box_paid);
		notPaidCheck = (CheckBox) findViewById(R.id.check_box_notpaid);
		creditSp = (Spinner) findViewById(R.id.sp_credit);
		monthSp = (Spinner) findViewById(R.id.sp_bill_month);
		Resources res = getContext().getResources();
		LayoutUtil.initToolbar(findViewById(R.id.tbar), res.getString(R.string.btn_filter),
				res.getString(R.string.back), this,
				res.getString(R.string.btn_OK), this);
		dbAdapter = new CreditsDbAdapter(getContext());
		initCreditSpinner();
		initBillMonthSpinner("-1");
		creditSp.setOnItemSelectedListener(this);
	}

	public void initCreditSpinner() {
		dbAdapter.open();
		List<Credit> credits = dbAdapter.fetchAllCredits();
		List<Map<String, String>> creditMapList = new ArrayList<Map<String, String>>();
		Map<String, String> map0 = new HashMap<String, String>();
		map0.put("id", "-1");
		map0.put("name",
				getContext().getResources().getString(R.string.allcredit));
		creditMapList.add(map0);
		for (Credit c : credits) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", String.valueOf(c.getId()));
			map.put("name", c.getName());
			creditMapList.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(getContext(), creditMapList,
				android.R.layout.simple_spinner_item, new String[] { "name" },
				new int[] { android.R.id.text1 });
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		creditSp.setAdapter(adapter);
		dbAdapter.close();
	}

	public void initBillMonthSpinner(String creditId) {
		dbAdapter.open();
		List<String> billMonths = dbAdapter.findAllBillMonth(creditId);
		List<Map<String, String>> monthMapList = new ArrayList<Map<String, String>>();
		for (String c : billMonths) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", c);
			map.put("name", c);
			monthMapList.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(getContext(), monthMapList,
				android.R.layout.simple_spinner_item, new String[] { "name" },
				new int[] { android.R.id.text1 });
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		monthSp.setAdapter(adapter);
		dbAdapter.close();
	}

	@SuppressWarnings("unchecked")
	public void onClick(View v) {
		String creditId = ((Map<String, String>) creditSp.getSelectedItem())
				.get("id");
		boolean paid = paidCheck.isChecked();
		boolean notpaid = notPaidCheck.isChecked();
		String billMonth = ((Map<String, String>) monthSp.getSelectedItem())
				.get("id");
		dismiss();
		callBack.filter(paid, notpaid, creditId, billMonth);
	}

	@SuppressWarnings("unchecked")
	public void onItemSelected(AdapterView<?> av, View arg1, int arg2, long arg3) {
		String creditId = ((Map<String, String>) av.getSelectedItem())
				.get("id");
		initBillMonthSpinner(creditId);
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// DO NOTHING
	}

	public interface CallBack {
		void filter(boolean paid, boolean notpaid, String creditId,
				String billMonth);
	}
}
